package client.gui;

import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import client.CheckinClient;
import client.ResolvedListener;
import client.Service;
import client.ServiceManager;
import client.ServiceRemovedEvent;
import client.ServiceResolvedEvent;
import client.ServiceType;
import client.gui.FlightInfoPanel;

public class MainClient extends JFrame implements ResolvedListener {
	private static final long serialVersionUID = 1L;
	/**
	 * The list of clients in the form of panels in tabs
	 * */	
	private ArrayList<DisplayPanel> myClientList;

	public JTabbedPane myTabbedPane = new JTabbedPane();

	private static ServiceManager myManager;

	public MainClient() {
		super("Smart Airport");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		myManager = new ServiceManager();
		myManager.addListener(this);
		myClientList = new ArrayList<DisplayPanel>();
		setBounds(0, 0, 800, 600);
		add(myTabbedPane);
		setVisible(true);
		setLocationRelativeTo(null);
		JPanel panel = new JPanel();
		ImageIcon icon = new ImageIcon("AP.jpg");
		JLabel label = new JLabel();
		JLabel l = new JLabel("Welcome to the Smart Airport");
		label.setIcon(icon);
		l.setSize(750, 30);
		panel.setLayout(new GridLayout(3,1));
		panel.add(l);
		panel.add(label);
		panel.add(new JLabel("Services will be added in tabs whe they become available."));
		myTabbedPane.addTab("Start", panel);
	}

	/**
	 * Fired when a service is resolved 
	 * */
	public void ServiceResolved(ServiceResolvedEvent s) {
		Service newService = s.getService();
		for (DisplayPanel client : myClientList) { 
			System.out.println(client.getType()+ "  == "+ newService.getType());
			if(client.getType() == newService.getType()){
				client.addService(newService);
				return;
			}
		}
		switch (newService.getType()) {
			case MusicServer:
				MusicPanel m = new MusicPanel(newService);
				myClientList.add(m);
				myTabbedPane.addTab("MusicServer", m);
				break;
			case OfficeServices:
				OfficePanel o = new OfficePanel(newService);
				myTabbedPane.addTab("Office Services", o);
				myClientList.add(o);
				break;
			case AutoCheckIn:
				CheckInPanel c = new CheckInPanel(newService);
				myTabbedPane.addTab("Check In", c);
				myClientList.add(c);
				break;
			case DutyFreeInfo:
				DutyFreePanel d = new DutyFreePanel(newService);
				myClientList.add(d);
				myTabbedPane.addTab("Duty Free", d);
				break;
			case ArrivalsInfo:
				FlightInfoPanel  f = new FlightInfoPanel(newService);
				myClientList.add(f);
				myTabbedPane.addTab("Flight Information Services", f);	
				break;
		} 
	}

	public static void main(String[] args) {
		MainClient client = new MainClient();
	}

	public void ServiceRemoved(ServiceRemovedEvent sre) {
		ServiceType t = null;
		if(sre.getService().getType().equals("MusicServer._tcp.local."))
			t = ServiceType.MusicServer;
		else if(sre.getService().getType().equals("OfficeServices._udp.local."))
			t = ServiceType.OfficeServices;
		else if(sre.getService().getType().equals("AutoCheckIn._udp.local."))
			t = ServiceType.AutoCheckIn;		
		else if(sre.getService().getType().equals("DutyFree._udp.local."))
			t = ServiceType.DutyFreeInfo;
		else if(sre.getService().getType().equals("FlightInfoServices._udp.local."))
			t = ServiceType.ArrivalsInfo;

		for (DisplayPanel client : myClientList) {
			System.out.println(client.getType()+" == "+t);
			if(client.getType() == t){
				if(client.removeService(sre.getService())){
					myTabbedPane.remove(client);
					myClientList.remove(client);
					break;
				}
				return;
			}
		}
		
	}

}
